package com.reone.imagewatcherpreview;

import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.AppCompatImageView;
import android.util.SparseArray;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.reone.imagewatcherpreview.util.ImagePreview;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    SparseArray<ImageView> sparseArray = new SparseArray<>();
    ImagePreview imagePreview;
    private static final String IMAGE_URL = "https://b-ssl.duitang.com/uploads/item/201603/11/20160311213223_RVhst.thumb.700_0.jpeg";
    private static final String IMAGE_URL2 = "https://b-ssl.duitang.com/uploads/item/201506/01/20150601023141_t4fYi.jpeg";
    private AppCompatImageView ivImage;
    private AppCompatImageView ivImage2;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ivImage = findViewById(R.id.iv_image);
        ivImage2 = findViewById(R.id.iv_image2);
        ivImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                App.talk("你点击了七代！");
                preview(0);
            }
        });
        ivImage2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                App.talk("你点击了好几代");
                preview(1);
            }
        });
        imagePreview = new ImagePreview(this);
        sparseArray.put(0, ivImage);
        sparseArray.put(1, ivImage2);
    }

    /**
     * 预览操作
     */
    private void preview(int pos) {
        List<Uri> urls = new ArrayList<>();
        urls.add(Uri.parse(IMAGE_URL));
        urls.add(Uri.parse(IMAGE_URL2));
        imagePreview.show(sparseArray.get(pos), sparseArray, urls);
    }

    /**
     * 加载图片
     */
    private void loadImage() {
        Glide.with(this).load(IMAGE_URL).into(ivImage);
        Glide.with(this).load(IMAGE_URL2).into(ivImage2);
    }

    @Override
    protected void onResume() {
        super.onResume();
        loadImage();
    }
}
